package com.lianniu.mgr.operMessage.service.impl;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.operMessage.dao.IOperMessageDao;
import com.lianniu.mgr.operMessage.service.IOperMessageService;
import com.lianniu.mgr.operMessage.vo.OperMessageConditionVo;
import com.lianniu.mgr.operMessage.vo.OperMessageVo;
import com.lianniu.mgr.operMessage.vo.OperTypeNumVo;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OperMessageServiceImpl implements IOperMessageService {

  private static final Logger logger = LoggerFactory.getLogger(OperMessageServiceImpl.class);
  @Autowired
  IOperMessageDao operMessageDao;

  /**
   * getOperMessageListForPage
   * 获取消息列表
   *
   * @param condition
   * @param paginationInfo
   * @return
   */
  @Override
  public BaseResult getOperMessageListForPage(OperMessageConditionVo condition, PaginationInfo paginationInfo) {
    condition.setPaginationInfo(paginationInfo);
    List<OperMessageVo> operMessageList = operMessageDao.getOperMessageListForPage(condition);
    Map<String, Object> retmap = new HashMap<String, Object>();
    retmap.put("total", paginationInfo.getTotalRecord());
    retmap.put("curPage", paginationInfo.getCurPage());
    retmap.put("rows", operMessageList);

    return BaseResult.success(retmap);
  }

  /**
   * @param operMessageVo
   * @return
   * @Title: insertOperMessage
   * @Description: 插入消息
   * @author: alex
   * @date: 2017年5月30日 上午9:25:12
   * @return: BaseResult
   */
  @Override
  public BaseResult insertOperMessage(OperMessageVo operMessageVo) {
    logger.info("insertOperMessage begin");
    logger.info("insertOperMessage: input data check begin");
    logger.info("insertOperMessage operMessageVo:" +JSON.toJSONString(operMessageVo));

    BaseResult baseResult;
    if (StringUtil.isEmpty(operMessageVo.getId())) {
      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    if (StringUtil.isEmpty(operMessageVo.getType())) {
      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    logger.info("insertDictionary: input data check end");
    return BaseResult.success(operMessageDao.insertOperMessage(operMessageVo));
  }

  /**
   * @param id
   * @return
   * @Title: updateOperMessageStatus
   * @Description: 更新消息只读状态
   * @author: alex
   * @date: 2017年5月30日 上午9:25:12
   * @return: BaseResult
   */
  @Override
  public BaseResult updateOperMessageRead(String id) {
    logger.info("updateOperMessageRead begin");
    logger.info("updateOperMessageRead id:" +id.toString());
    logger.info("updateOperMessageRead: input data check begin");
    BaseResult baseResult;
    if (StringUtil.isEmpty(id.toString())) {
      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    logger.info("updateOperMessageRead: input data check end");
    return BaseResult.success(operMessageDao.updateOperMessageIsRead(id));
  }

  @Override
  public BaseResult getOperTypeNum() {
   List<OperTypeNumVo> curOperTypeNum = operMessageDao.getOperTypeNum();
    return BaseResult.success(curOperTypeNum);
  }


}
